package com.whx.authorization.web.service;

import com.whx.authorization.constant.AuthorizationConstant;
import com.whx.authorization.web.domain.LogoutInfo;
import com.whx.authorization.util.SimpleHttpUtil;
import com.whx.authorization.util.UrlParamUtil;
import org.jsoup.Connection;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

/**
 * 类名：AuthorizationService
 * 类路径： com.whx.authorization.web.service.AuthorizationService
 * 创建者： whx
 * 创建时间： 10/8/18 3:08 PM
 * 项目： authorization_center
 * 描述：
 */
public class AuthorizationService {

    public static void subSystemLogout(Collection<LogoutInfo> logoutInfoCollection) {
        if (logoutInfoCollection != null && !logoutInfoCollection.isEmpty()) {
            for (LogoutInfo logout : logoutInfoCollection) {
                // 子系统用户退出地址
                String logoutUrl = logout.getLogoutUrl();
                // 子系统客户端session清除用param参数
                String logoutParams = logout.getLogoutParams();
                // 子系统客户端session清除用cookie
                String cookieString = logout.getCookieString();
                // http请求方式
                String httpMethod = logout.getHttpMethod();
                Map<String, String> cookieMap = UrlParamUtil.parseCookies(cookieString);
                Map<String, String> paramsMap = UrlParamUtil.parseQueryParams(logoutParams);
                try {
                    Connection.Method method = AuthorizationConstant.HttpMethod.METHOD_POST.equals(httpMethod)? Connection.Method.POST: Connection.Method.GET;
                    SimpleHttpUtil.execute(logoutUrl, paramsMap, null, cookieMap, method);
                } catch (IOException ignore) {
                }
            }
        }
    }

}
